package com.tbit.uqbike.client.util;


import com.alibaba.fastjson.JSON;
import com.braintreegateway.*;
import com.tbit.uqbike.client.constant.SystemData;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;


/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 17:43 2021/3/5
 */
@Slf4j
public class PaypalUtils {

    /**
     * 初始化paypal连接
     *
     * @param accountId  品牌id
     * @param type       类型
     * @param merchantId 商户id
     * @param publicKey  公钥
     * @param privateKey 私钥
     * @return
     */
    public static BraintreeGateway init(Integer accountId, String type, String merchantId, String publicKey, String privateKey) {
        BraintreeGateway gateway = SystemData.braintreeGatewayMap.get(String.valueOf(accountId));
        if (gateway == null) {
            if (type.equalsIgnoreCase("sandbox")) {
                log.error("初始化沙盒连接");
                gateway = new BraintreeGateway(Environment.SANDBOX, merchantId, publicKey, privateKey);
            } else {
                log.error("初始化live连接");
                gateway = new BraintreeGateway(Environment.PRODUCTION, merchantId, publicKey, privateKey);
            }
            SystemData.braintreeGatewayMap.put(String.valueOf(accountId), gateway);
        }
        return gateway;
    }

    /**
     * 扣费
     *
     * @param money
     * @param nonce
     * @return
     */
    public static String cash(BraintreeGateway gateway, Double money, String nonce, String orderNO) {
        System.out.println("收到金额=" + money);
        TransactionRequest request = new TransactionRequest()
                .amount(new BigDecimal(money).setScale(2, BigDecimal.ROUND_HALF_UP))
                .currencyIsoCode("USD")
                .paymentMethodNonce(nonce)
                .orderId(orderNO)
                .deviceData("app")
                .options()
                .submitForSettlement(true)
                .done();
        Result<Transaction> saleResult = gateway.transaction().sale(request);
        if (saleResult.isSuccess()) {
            Transaction transaction = saleResult.getTarget();
            log.info(transaction.toString());
            System.out.println("Success ID: " + transaction.getId());
            System.out.println("transaction ======  " + JSON.toJSON(transaction));
            return transaction.getId();
        } else {
            System.out.println(saleResult.getMessage());
            //支付失败的情况
            log.error("Message:" + saleResult.getMessage());
            log.error("Error:" + saleResult.getErrors().toString());
            log.error("Error-JSON:" + JSON.toJSON(saleResult.getErrors()));
            return null;
        }
    }

  /*private static BraintreeGateway gateway = new BraintreeGateway(
      Environment.SANDBOX,
      "s6x9ksmr2gg6wtnr",
      "xyxxqqhtjppv6rh4",
      "79e330d1ac207007d34fa7c71bc156c2"
  );*/

    public static void main(String[] args) {
        BraintreeGateway gateway = new BraintreeGateway(
                Environment.SANDBOX,
                "s6x9ksmr2gg6wtnr",
                "xyxxqqhtjppv6rh4",
                "79e330d1ac207007d34fa7c71bc156c2"
        );

        // pass clientToken to your front-end
    /*String clientToken = gateway.clientToken().generate();
    System.out.println(clientToken);*/

        /*System.out.println(new BigDecimal(0.01));*/
        TransactionRequest request = new TransactionRequest()
                .amount(new BigDecimal(0.01).setScale(2, BigDecimal.ROUND_HALF_UP))
                .paymentMethodNonce("85a52e6a-2128-053d-6bbc-51b31247610b")
                .deviceData("app")
                .options()
                .submitForSettlement(true)
                .done();

        Result<Transaction> saleResult = gateway.transaction().sale(request);
        if (saleResult.isSuccess()) {
            Transaction transaction = saleResult.getTarget();

            System.out.println("Success ID: " + transaction.getId());
            System.out.println("transaction ======  " + JSON.toJSON(transaction));
        } else {
            System.out.println(saleResult.getMessage());
            //支付失败的情况
            log.error("Message: {}" + saleResult.getMessage());
            log.error("Error: {}" + saleResult.getErrors().toString());
            log.error("Error-JSON: {}" + JSON.toJSON(saleResult.getErrors()));
        }

    /*Transaction transaction = gateway.transaction().find("23zmw91z");
    System.out.println(transaction.getStatus());*/
    /*Result<Transaction> result = gateway.transaction().refund("3ybeywga");
    Transaction transaction = result.getTarget();
    System.out.println(JSON.toJSON(transaction));*/
    }


}
